﻿using Qiniu.Storage;
using Qiniu.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcUploadImgageBase64ToQiNiuCloud.Controllers
{

    /*
     * 官方文档说明： https://developer.qiniu.com/kodo/sdk/1237/csharp
     * 
     说明： 
     工具-》nuget包管理器--》程序包管理器控制台
     输入： 
     Install-Package Newtonsoft.Json
     回车，安装完成后再输入：
     Install-Package Qiniu
     回车

         */
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            ViewData["qntoken"] = GetQiNunToken("存储空间名");
            return View();
        }
        
        /// <summary>
        /// 获取上传凭据
        /// </summary>
        /// <param name="blue"></param>
        /// <returns></returns>
        public string GetQiNunToken(string blue)
        {
            //生产凭据参考： 
            //https://developer.qiniu.com/kodo/sdk/1237/csharp#simple-uptoken
           

            Mac mac = new Mac("AccessKey", "SecretKey");
            PutPolicy putPolicy = new PutPolicy();
            putPolicy.Scope = blue;
            putPolicy.SetExpires(7200);
            string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());
            return token;
        }
    }
}